package com.zhiwang.planet.ui.loginv2

import android.os.Bundle
import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProviders
import com.zhiwang.planet.R
import com.zhiwang.planet.databinding.ActivityWxloginBinding
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.mvvm.viewmodel.loginv2.WxLoginViewModel
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.util.*
import kotlinx.android.synthetic.main.activity_wxlogin.*
import org.greenrobot.eventbus.EventBus
import javax.inject.Inject

class WxLoginActivity : BaseActivity() {


    @Inject
    lateinit var restApi: RestApi

    lateinit var viewModel: WxLoginViewModel

    val TAG = "WxLoginActivity"

    var token = ""

    override fun initInjection() {
        super.initInjection()
        DaggerUserComponent.builder()
                .appComponent(getAppComponent())
                .autoDisposeModule(AutoDisposeModule(this))
                .accountModule(AccountModule(this))
                .build()
                .inject(this)

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil
                .setContentView<ActivityWxloginBinding>(this, R.layout.activity_wxlogin)
        setSupportActionBar(toolbar)
        Utils.setStatusBar(this, false, false)
        viewModel = ViewModelProviders.of(this).get(WxLoginViewModel::class.java)

        if (intent.hasExtra("token"))
            token = intent.getStringExtra("token")
        if (intent.hasExtra("bindMode"))
            viewModel.bindMode.set(intent.getBooleanExtra("bindMode", false))
        if (intent.hasExtra("code"))
            viewModel.wxCode = intent.getStringExtra("code")
        binding.lifecycleOwner = this
        binding.viewModel = viewModel
        viewModel.context = this
        viewModel.restApi = restApi
        viewModel.errHandler = this::onError
        viewModel.start()


        if (AppHelper.isDev()) {
            toolbar.setOnLongClickListener {
                val env = AppHelper.switchEnv()
                if (env) {
                    Toaster.message(getString(R.string.test_env_msg))
                } else {
                    Toaster.message(getString(R.string.product_env_msg))
                }
                TokenManager.clearToken()
                return@setOnLongClickListener true
            }
        }

        AppUtil.getDeviceId {
            viewModel.imei = it
            TokenManager.saveDevice(it)
        }


        phoneEditor.addTextChangedListener {

            val phone = it.toString()

            login_btn.isEnabled = phone.length > 0

        }

        Utils.setStatusBar(this,false,false)
        Utils.setStatusTextColor(true,this)

    }


}